using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;

/// <summary>
/// Unity编辑器Scene窗口扩展
/// </summary>
[CustomEditor(typeof(DrawGizmos))]
public class SceneExtention : Editor
{
    [InitializeOnLoadMethod]
    static void Initialize()
    {
        SceneView.duringSceneGui += OnSceneContextMenu;
        SceneView.duringSceneGui += OnSceneGUI;
    }

    private static int i;
    static void OnSceneContextMenu(SceneView view)
    {
        if (Event.current != null && Event.current.button == 1 && Event.current.type == EventType.MouseUp)
        {
            Debug.Log("MouseR Up");

            Vector3 pos = Event.current.mousePosition;
            pos.y -= 100;
            var rect = new Rect(pos, new Vector2(100, 100));
            GUIContent[] contents =
            {
                new GUIContent() {text = "click me"},
                new GUIContent() {text = "click this"},
            };

            EditorUtility.DisplayCustomMenu(rect, contents, i, OnMenu, null);
            Event.current.Use();
        }
    }

    private static void OnMenu(object userdata, string[] options, int selected)
    {
        i = selected;
        var optionNames = string.Join(",", options);
        Debug.Log($"You chose {selected} in {optionNames}");
    }
    
    // =======================Draw Something In Scene Window==================

    /// <summary>
    /// Editor Feature
    /// </summary>
    private void OnPreSceneGUI()
    {
        Handles.BeginGUI();
        if (GUILayout.Button("Do Something",GUILayout.Width(100)))
        {
            var dg = target as DrawGizmos;
            // do something
        }
        Handles.EndGUI();
    }

    private static void OnSceneGUI(SceneView obj)
    {
        Handles.BeginGUI();
        if (GUILayout.Button("Do Something(Long Time)",GUILayout.Width(200)))
        {
            // do something
        }
        Handles.EndGUI();
    }
}